package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.OlderWeight;
import com.woniuxy.oldmanapi.service.WeightService;
import com.woniuxy.oldmanapi.vo.HeightVo;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.vo.WeightVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class WeightController {

    @Autowired
    WeightService weightService ;

    @GetMapping("/weight/list")
    public ResponseResult<PageInfo<WeightVo>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "9") Integer size,
            String name){
        ResponseResult<PageInfo<WeightVo>> result = new ResponseResult<>();
        try {
            PageInfo<WeightVo> lists = weightService.findWeightByPage(now,size,name);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @PostMapping("/weight/add")
    public ResponseResult<Void> add(@RequestBody OlderWeight olderWeight){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            weightService.add(olderWeight);
            result.setCode(200);
            result.setMsg("添加成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result ;
    }
    @PutMapping("/weight/modify")
    public ResponseResult<Void> modify(@RequestBody OlderWeight olderWeight){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            weightService.modify(olderWeight);
            result.setCode(200);
            result.setMsg("修改成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result ;
    }
    @DeleteMapping("/weight/delete/{id}")
    public ResponseResult<Void> delete(@PathVariable("id")int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            weightService.delete(id);
            result.setCode(200);
            result.setMsg("删除成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result ;
    }

    @GetMapping("/weight/one/{id}")
    public ResponseResult<WeightVo> one(@PathVariable("id") int id) {
        ResponseResult<WeightVo> result = new ResponseResult<>();
        try {
            WeightVo weightVo = weightService.selectOne(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(weightVo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
}
